package com.mbbb.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mbbb.dao.SetmealDao;
import com.mbbb.pojo.Setmeal;
import com.mbbb.service.SetmealService;
import com.mbbb.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService{

    @Autowired
    SetmealDao setmealDao;

    @Override
    public void add(Integer[] travelgroupIds, Setmeal setmeal) {
        setmealDao.add(setmeal);

        //主键回填：添加表单数据到数据库，MyBatis可以获取自动分配的主键，并赋值给实体对象
        Integer setmealId = setmeal.getId();

        setSetmealAndTravelGroup(setmealId,travelgroupIds);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page page = setmealDao.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    private void setSetmealAndTravelGroup(Integer setmealId, Integer[] travelgroupIds) {
        if (travelgroupIds!=null && travelgroupIds.length>0){
            for (Integer travelgroupId : travelgroupIds) {

                setmealDao.setSetmealAndTravelGroup(setmealId,travelgroupId);
            }
        }
    }
}
